Fk:loadTranslationTable{
  ["plum__shuangjian"] = "霜简",
  [":plum__shuangjian"] = "若你的手牌数等于本回合此技能的发动次数，则你可以将中央区内的一张即时牌置于牌堆顶，视为使用或打出之。若如此做，其他角色在此牌结算中也可以如此做。",
  ["#plum__shuangjian"] = "霜简：可以将中央区内的一张即时牌置于牌堆顶，视为使用或打出之",

  ["@@plum__shuangjian"] = "霜简",
  ["#plum__shuangjian-put"] = "霜简：将中央区内的一张%arg置于牌堆顶",
}

local DIY = require "packages/diy_utility/diy_utility"

local function isImmediate(id)
  return Fk:getCardById(id).type == Card.TypeBasic or Fk:getCardById(id):isCommonTrick()
end

local shuangjian = fk.CreateSkill{
  name = "plum__shuangjian",
}

--- acquire
shuangjian:addAcquireEffect(function (self, player)
  local room = player.room
  room:addSkill("#CenterArea")
end)

--- view as
shuangjian:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,trick",
  prompt = "#plum__shuangjian",
  expand_pile = function (self, player)
    return DIY.getCenterCards(Fk:currentRoom())
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and isImmediate(to_select) and table.contains(DIY.getCenterCards(Fk:currentRoom()), to_select)
  end,
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local to_use = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    to_use.skillName = shuangjian.name
    to_use:addFakeSubcards(cards)
    return to_use
  end,
  before_use = function (self, player, use)
    local room = player.room
    room:moveCards({
      ids = use.card.fake_subcards,
      from = player,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      skillName = shuangjian.name,
      proposer = player,
      moveVisible = true,
    })
    local j = false
    local event = room.logic:getCurrentEvent()
    while true do
      local p = event:findParent(GameEvent.UseCard)
      if not p then
        j = true
        break
      end
      event = p
      if event.data.card.skillName == shuangjian.name then
        break
      end
    end
    if j then
      use.extra_data = { plum__shuangjian = true }
      for _, p in ipairs(room:getOtherPlayers(player, true)) do
        room:handleAddLoseSkills(p, "plum__shuangjian_derive")
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:getHandcardNum() == player:usedSkillTimes(shuangjian.name, Player.HistoryTurn)
  end,
  enabled_at_response = function (self, player, response)
    return player:getHandcardNum() == player:usedSkillTimes(shuangjian.name, Player.HistoryTurn)
  end,
  enabled_at_nullification = function (self, player, data)
    if table.find(DIY.getCenterCards(Fk:currentRoom()), function (id) return Fk:getCardById(id).trueName == "nullification" end) then
      return true
    end
  end,
})

--- clear
shuangjian:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill("plum__shuangjian_derive") and data.extra_data and data.extra_data.plum__shuangjian
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "-plum__shuangjian_derive")
  end
})

return shuangjian